package com.hzeport.bgs22emduty.outbound.db;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzeport.bgs22emduty.config.exception.HdcException;
import com.hzeport.bgs22emduty.entity.ParamHolidays;
import com.hzeport.bgs22emduty.enums.HolidaysEnum;
import com.hzeport.bgs22emduty.enums.STypeEnum;
import com.hzeport.bgs22emduty.outbound.ParamHolidaysRepository;
import com.hzeport.bgs22emduty.outbound.db.mapper.ParamHolidaysMapper;
import com.hzeport.bgs22emduty.common.base.BaseRepositoryImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 假期年度：
    说明假期使用的年度

假期日期：
    年度假期的具体日期，yyyy-MM-dd
 *
 * @author code generator
 * @date 2022-12-02 11:21:59
 */
@Service
@Slf4j
public class ParamHolidaysRepositoryImpl extends BaseRepositoryImpl<ParamHolidaysMapper, ParamHolidays> implements ParamHolidaysRepository {

    @Override
    public STypeEnum deriveCurSType(String customsId, LocalDateTime currentTime) {

        final ParamHolidays record = getOne(new LambdaQueryWrapper<ParamHolidays>()
                .eq(ParamHolidays::getCustomId, customsId)
                .eq(ParamHolidays::getHolidayDate, currentTime.toLocalDate()), false);
        if (null != record) {
            final String holidayType = record.getHolidayType();
            if (StringUtils.equals(HolidaysEnum.HOLIDAY.getValue(), holidayType)) {
                return STypeEnum.TYPE_ZMB;
            } else if (StringUtils.equals(HolidaysEnum.IMPORTANT_PERIOD.getValue(), holidayType)) {
                return STypeEnum.TYPE_ZYB;
            } else if (StringUtils.equals(HolidaysEnum.HOLIDAY_EXCHANGE.getValue(), holidayType)) {
                return STypeEnum.TYPE_WB;
            }
        }
        switch (currentTime.getDayOfWeek()) {
            case SUNDAY:
            case SATURDAY:
                return STypeEnum.TYPE_ZM;
            case MONDAY:
            case TUESDAY:
            case WEDNESDAY:
            case THURSDAY:
            case FRIDAY:
                return STypeEnum.TYPE_WB;
            default:
                throw new HdcException("未能正确判断当前值班类型");
        }
    }
}
